package com.hanxiang.hankol.common

import com.baselibrary.router.ActivityRouter
import android.annotation.SuppressLint
import androidx.viewpager.widget.ViewPager
import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.utils.StatusBarUtil
import com.hanxiang.hankol.R
import com.hanxiang.hankol.databinding.ActivityBigImagerBinding
import com.media.publish.fragment.ViewImagerFragment
import java.util.ArrayList

/*大图模式*/
@Route(path = ActivityRouter.ACTIVITY.BROWSE_MEDIA)
class BigImgViewActivity : BaseActivity() {
    private lateinit var mBinding: ActivityBigImagerBinding
    private var mUrls: ArrayList<String> = ArrayList()
    private var mFragments: ArrayList<Fragment> = arrayListOf()
    private var mIndex = 0
    private var mPictureWatermark: String? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_big_imager
    }

    override fun init() {
        setStatusBarDarkTheme(false)
        StatusBarUtil.setTranslucentStatus(this)
        mBinding = getDataBinding()
        initView()
        initData()
        loaData()
        initListener()
    }

    @SuppressLint("ResourceType")
    private fun initView() {
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        mUrls.addAll(intent.getStringArrayListExtra(Constants.WIKI_PICTURE_LIST)!!)
        mIndex = intent.getIntExtra(Constants.WIKI_PICTURE_INDEX, 0)
        mPictureWatermark = intent.getStringExtra(Constants.WIKI_PICTURE_WATERMARK)
        if (mUrls.isNullOrEmpty()) {
            return
        }
        for (i in mUrls.indices) {
            mFragments.add(ViewImagerFragment.newInstance(setArguments(i)))
        }
        mBinding.tvTitleContent.text = mIndex.toString() + "/" + mUrls.size
    }

    private fun loaData() {
        mBinding.vpImager.offscreenPageLimit = mFragments.size
        mBinding.vpImager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            //选中的item
            override fun getItem(position: Int): Fragment {
                return mFragments[position]
            }

            override fun getCount(): Int {
                return mFragments.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return ""
            }
        }
        mBinding.vpImager.setCurrentItem(mIndex, true)
    }

    private fun initListener() {
        mBinding.ivTitleClose.setOnClickListener { finish() }
        mBinding.vpImager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            @SuppressLint("SetTextI18n")
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {
                mBinding.tvTitleContent.text = (i + 1).toString() + "/" + mUrls.size
            }

            override fun onPageSelected(pos: Int) {

            }
            override fun onPageScrollStateChanged(i: Int) {

            }
        })
    }

    private fun setArguments(i: Int): Bundle {
        val mUrl = mUrls[i]
        val mBundle = Bundle()
        val type: Int =
            if (!TextUtils.isEmpty(mUrl) && mUrl.contains("videotx") && !mUrl.endsWith(".jpg") && !mUrl.endsWith(".png") && !mUrl.endsWith(".jpeg") && !mUrl.endsWith(".bmp")) {
                ViewImagerFragment.VIDEO_TYPE
            } else {
                ViewImagerFragment.IMG_TYPE
            }
        mBundle.putString("url", mUrls[i])
        mBundle.putInt(Constants.WIKI_IMGPAGER_TYPE, type)
        mBundle.putString(Constants.WIKI_PICTURE_WATERMARK, mPictureWatermark)
        return mBundle
    }
}